﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Infrastructure;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class AdvertisementBlockDao : AbstractDao<AdvertisementBlock>, IAdvertisementBlockDao
    {
        public override bool
        create(AdvertisementBlock adsBlock)
        {
            try
            {
                adsBlock.blkId = this.createPrefixId();

                _ctx.AdvertisementBlocks.InsertOnSubmit(adsBlock);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override bool
        update(AdvertisementBlock adsBlock)
        {
            try
            {
                var curr = getById(adsBlock.blkId);

                curr.blkName = adsBlock.blkName;
                curr.blkDescription = adsBlock.blkDescription;
                curr.blkWidth = adsBlock.blkWidth;
                curr.blkHeight = adsBlock.blkHeight;
                curr.blkActive = adsBlock.blkActive;

                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public string
        createPrefixId()
        {
            string currMaxId = getAll().Max(i => i.blkId);
            string prefixPart = "blk";
            int nDigits = 2;

            AutoId autoid = new AutoId();

            return autoid.tryCreatePrefixId(currMaxId, prefixPart, nDigits);
        }

        public override AdvertisementBlock
        getById(object id)
        {
            return getSingle(i => i.blkId.Equals((string)id));
        }

        public List<AdvertisementBlock>
        getByName(string blkName)
        {
            try
            {
                return getMultiple(i => i.blkName.Contains(blkName));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<AdvertisementBlock>
        getBySize(int blkWidth, int blkHeight)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public List<AdvertisementBlock>
        getBySize(bool isWidth, int blkSize)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public List<AdvertisementBlock>
        getByActive(bool blkActive)
        {
            try
            {
                return getMultiple(i => i.blkActive == blkActive);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region IAdvertisementBlockDao

    public interface IAdvertisementBlockDao : IDao<AdvertisementBlock>
    {
        String createPrefixId();

        List<AdvertisementBlock> getByName(String blkName);

        List<AdvertisementBlock> getBySize(int blkWidth, int blkHeight);

        List<AdvertisementBlock> getBySize(Boolean isWidth, int blkSize);

        List<AdvertisementBlock> getByActive(Boolean blkActive);
    }
    #endregion
}